package com.qiyeji.real_estate.controller;


import com.qiyeji.real_estate.entity.accountInformation;
import com.qiyeji.real_estate.entity.accountInformationKey;
import com.qiyeji.real_estate.service.LoginService;
import com.qiyeji.real_estate.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RequestMapping("/Login")
@RestController
public class LoginController {

    @Autowired
    LoginService loginService;

    @PostMapping("/Sign_In")
    public Result checkPassWord(@RequestBody Map<String, String> params) {
        String id = params.get("username");
        String passWord = params.get("password");
        accountInformationKey accountInformationKey = new accountInformationKey(id);
        String temp;
        try {
            temp = loginService.Login(accountInformationKey);
        } catch (Exception e) {
            return Result.usernameError();
        }
        if (passWord.equals(temp)) {
            return Result.success(true);
        }
        else
            return Result.passwordError();

    }

    @PostMapping("/Register")
    public Result registerID(@RequestBody Map<String, String> params) {
        String id = params.get("ID");
        String sCode = params.get("sCode");
        String passWord = params.get("passWord");
        String power = params.get("power");
        accountInformation accountInformation = new accountInformation(id, sCode, passWord, power);
        Integer temp;
        try{
            temp = loginService.registerID(accountInformation);
        } catch (Exception e){
            return Result.error();
        }
        if (temp > 0) {
            return Result.success(true);
        }
        else
            return Result.success(false);
    }

    @PostMapping("/Alter")
    public Result alterPassWord(@RequestBody Map<String, String> params) {
        String id = params.get("ID");
        String sCode = params.get("sCode");
        String passWord = params.get("passWord");
        String power = params.get("power");
        accountInformation accountInformation = new accountInformation(id, sCode, passWord, power);
        Integer temp;
        try {
            temp = loginService.alterPassWord(accountInformation);
        } catch (Exception e) {
            return Result.error();
        }
        if (temp > 0) {
            return Result.success(true);
        }
        else
            return Result.success(false);
    }

    @PostMapping("/loginPower")
    public Result loginPower(@RequestBody Map<String,String> params){
        String id = params.get("username");
        String password = params.get("password");
        String power = loginService.loginPower(id,password);
        if(power==null) {
            return Result.error();
        }
        else {
            return Result.success(power);
        }
    }

    @PostMapping("/GetAllAccountin")
    public Result getAllAccountin() {
        List<accountInformation> res = loginService.getAllAccountin();

        if (res == null)
            return Result.error("不存在任何账号！");
        else
            return Result.success(res);
    }

    @PostMapping("/Delete")
    public Result deleteAccountin(@RequestBody Map<String, String> params) {
        Integer res = loginService.delete(new accountInformationKey(params.get("ID+"), params.get("sCode")));

        if (res == 0) {
            return Result.error("删除账号失败！");
        }else
            return Result.success(res);
    }

}
